home *** CD-ROM | disk | FTP | other *** search
- /*
- * $RCSfile: mutex.c,v $
- * $Revision: 1.1.1.1 $
- * $Date: 1996/05/04 21:55:40 $
- */
- /**********************************************************************
- * EXODUS Database Toolkit Software
- * Copyright (c) 1991 Computer Sciences Department, University of
- * Wisconsin -- Madison
- * All Rights Reserved.
- *
- * Permission to use, copy, modify and distribute this software and its
- * documentation is hereby granted, provided that both the copyright
- * notice and this permission notice appear in all copies of the
- * software, derivative works or modified versions, and any portions
- * thereof, and that both notices appear in supporting documentation.
- *
- * THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY OF WISCONSIN --
- * MADISON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.
- * THE DEPARTMENT DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES
- * WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
- *
- * The EXODUS Project Group requests users of this software to return
- * any improvements or extensions that they make to:
- *
- * EXODUS Project Group
- * c/o David J. DeWitt and Michael J. Carey
- * Computer Sciences Department
- * University of Wisconsin -- Madison
- * Madison, WI 53706
- *
- * or exodus@cs.wisc.edu
- *
- * In addition, the EXODUS Project Group requests that users grant the
- * Computer Sciences Department rights to redistribute these changes.
- **********************************************************************/
- #include "queue_consist.h"
- #include "mutex_compile.h"
-
- #include "ess.h"
- #include "checking.h"
- #include "sysdefs.h"
- #include "list.h"
- #include "error.h"
- #include "trace.h"
- #include "tid.h"
- #include "pool.h"
- #include "io.h"
- #include "bitvec.h"
- #include "lock.h"
- #include "msgdefs.h"
- #include "disk.h"
- #include "thread.h"
- #include "semaphore.h"
- #include "latch.h"
- #include "bf.h"
- #include "link.h"
- #include "volume.h"
- #include "trace.h"
- #include "msgvector.h"
- #include "disk_funcs.h"
- #include "disk_globals.h"
- #include "io_globals.h"
- #include "msg_globals.h"
- #include "sharedmem_globals.h"
-
- #ifdef DEBUG
- static int spin_count[7];
-
- void
- dump_spin()
- {
- { register int i, j;
- for(i=1, j=0; i < (sizeof(spin_count)/sizeof(int)); i++) {
- j+= spin_count[i];
- }
- if(j == 0) /* don't bother to print anything: they're all
- in the first category */
- return;
- }
- fprintf(stderr,
- "%s: Histogram of number of spins:\n",
- process_identity.me==SERVER_PROC?"SERVER":"DISKRW");
- fprintf(stderr,
- "%8.8s | %8.8s | %8.8s | %8.8s | %8.8s | %8.8s | %8.8s\n",
- "<10", "<10**3", "<10**4", "<10**5", "<10**6", "<10**7", ">10**7");
- fprintf(stderr,
- "%8d | %8d | %8d | %8d | %8d | %8d | %8d\n",
- spin_count[0], spin_count[1], spin_count[2], spin_count[3],
- spin_count[4], spin_count[5], spin_count[6]);
- } /* dump_spin */
- #endif DEBUG
-
- void
- getMutex(
- MUTEX *qp
- )
- {
- #ifdef DEBUG
- register int spins=0;
- #endif DEBUG
-
- #ifdef TESTANDSET
- TRPRINT(TR_DISKRW, TR_LEVEL_3, ("%s in getMutex(0x%x) sync %d",
- process_identity.me==SERVER_PROC?"server":"diskrw", qp,
- qp->sync
- ));
- #else TESTANDSET
- TRPRINT(TR_DISKRW, TR_LEVEL_3, ("%s in getMutex(0x%x) sync %d",
- process_identity.me==SERVER_PROC?"server":"diskrw", qp,
- (qp)->flag[process_identity.me]
- ));
- #endif TESTANDSET
-
- CHECK_MUTEX_MAGIC(qp);
- #ifdef TESTANDSET
- while (testandset(&qp->sync, 1) != 0)
- #else TESTANDSET
- qp->flag[process_identity.me] = 1; /* want the cs */
- qp->turn = process_identity.other; /* give him a chance */
- while(qp->flag[process_identity.other] &&
- (qp->turn == process_identity.other))
- #endif TESTANDSET
- {
- #ifdef DEBUG
- spins++;
- #endif DEBUG
- }
-
- #ifdef DEBUG
- if(spins < 10) spin_count[0]++;
- else if(spins < 1000) spin_count[1]++;
- else if(spins < 10000) spin_count[2]++;
- else if(spins < 100000) spin_count[3]++;
- else if(spins < 1000000) spin_count[4]++;
- else if(spins < 10000000) spin_count[5]++;
- else spin_count[6]++;
- TRPRINT(TR_DISKRW, TR_LEVEL_3, ("%s got Mutex after %d spins",
- process_identity.me==SERVER_PROC?"server":"diskrw", spins));
-
-
- #define MILLION 1000000
- SM_ASSERT(LEVEL_1, (spins < 720*MILLION));
-
- #else
- TRPRINT(TR_DISKRW, TR_LEVEL_3, ("%s got Mutex ",
- process_identity.me==SERVER_PROC?"server":"diskrw"));
-
- #endif DEBUG
-
- } /* getMutex */
-
-
- /*
- * returns 1 for success, 0 for failure
- */
- int
- tryMutex(
- MUTEX *qp
- )
- {
- CHECK_MUTEX_MAGIC(qp);
- #ifdef TESTANDSET
- TRPRINT(TR_DISKRW, TR_LEVEL_3, ("%s in tryMutex(0x%x) sync %d",
- process_identity.me==SERVER_PROC?"server":"diskrw", qp,
- qp->sync));
- return (testandset(&qp->sync, 1) == 0)? 1 : 0;
- #else TESTANDSET
- qp->flag[process_identity.me]=1; /* want the cs */
- qp->turn = process_identity.other; /* give him a chance */
- if(qp->flag[process_identity.other] &&
- (qp->turn == process_identity.other)) {
- qp->flag[process_identity.me]=0; /* don't want it anymore */
- return 0;
- }
- return 1;
- #endif TESTANDSET
- } /* tryMutex */
-
- void
- giveMutex(
- MUTEX *qp
- )
- {
-
- #ifdef TESTANDSET
- qp->sync = 0;
- #else TESTANDSET
- (qp)->flag[process_identity.me]=0; /* i'm out of cs & don't want it */
- #endif TESTANDSET
-
- #ifdef TESTANDSET
- TRPRINT(TR_DISKRW, TR_LEVEL_3, ("%s gave up Mutex(0x%x) sync=%d ",
- process_identity.me==SERVER_PROC?"server":"diskrw", qp,
- qp->sync
- ));
- #else TESTANDSET
- TRPRINT(TR_DISKRW, TR_LEVEL_3, ("%s gave up Mutex(0x%x) sync=%d ",
- process_identity.me==SERVER_PROC?"server":"diskrw", qp,
- (qp)->flag[process_identity.me]
- ));
- #endif TESTANDSET
- CHECK_MUTEX_MAGIC(qp);
- } /* giveMutex */
-
-